/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rdb.sbean;

import com.rdb.entities.*;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Bich Tran Van
 */
@Stateless
public class StudentsFacade extends AbstractFacade<Students> {
    @PersistenceContext(unitName = "ResultDeclarationBoard-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public StudentsFacade() {
        super(Students.class);
    }
    
    public List<Students> getStudentInExam(String enroll, String name, int classid){
        if(name == null){
            name ="";
        }
        if(enroll == null){
            enroll = "";
        }
        String query = "SELECT s FROM Students s WHERE s.enRoll LIKE :enRoll and s.stuName LIKE :stuName";
        if(classid != 0){
            query = query + " and s.classId.id = :classId";
        }
        javax.persistence.Query q = getEntityManager().createQuery(query);
        q.setParameter("enRoll", '%' + enroll + '%');
        q.setParameter("stuName", '%' + name + '%');
        if(classid !=0){
        q.setParameter("classId", classid);
        }
        System.out.print("Result  " + q.getResultList().size() +" " +enroll +" " + name + " " + classid);
        return q.getResultList();
    }
    
    public List<Students> getAllInClass(int classId){
        String sql = "SELECT s FROM Students s WHERE s.classId.id = :classId";
         javax.persistence.Query q = getEntityManager().createQuery(sql);
        q.setParameter("classId", classId);
        return q.getResultList();
    }
    
}
